<?php

class ProductsController extends AdminController {

    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    public function actionView($id) {
        $this->render('view', array(
            'model' => $this->loadModel($id),
        ));
    }

    public function actionCreate() {
        $model = new site_products;

        if (isset($_POST['site_products'])) {
            $model->attributes = $_POST['site_products'];
            $model->products_input_datetime = date("Y-m-d H:i:s");

            /* penanganan thumbnail */
            $thumbnail_file = CUploadedFile::getInstance($model, 'products_thumbnail');
            // has the user uploaded a new file?
            if ($thumbnail_file) {
                if (!is_dir(Yii::getPathOfAlias('webroot') . '/images/products/' . $model->products_thumbnail)) {
                    mkdir(Yii::getPathOfAlias('webroot') . '/images/products/' . $model->products_thumbnail);
                    chmod(Yii::getPathOfAlias('webroot') . '/images/products/' . $model->products_thumbnail, 0777);
                }
                $model->products_thumbnail = $thumbnail_file->name;
                $thumbnail_file->SaveAs(Yii::getPathOfAlias('webroot') . '/images/products/' . $thumbnail_file->name);
            }

            /* penanganan image */
            $image_file = CUploadedFile::getInstance($model, 'products_image');
            // has the user uploaded a new file?
            if ($image_file) {
                if (!is_dir(Yii::getPathOfAlias('webroot') . '/images/products/' . $model->products_image)) {
                    mkdir(Yii::getPathOfAlias('webroot') . '/images/products/' . $model->products_image);
                    chmod(Yii::getPathOfAlias('webroot') . '/images/products/' . $model->products_image, 0777);
                }
                $model->products_image = $image_file->name;
                $image_file->SaveAs(Yii::getPathOfAlias('webroot') . '/images/products/' . $image_file->name);
            }

            if ($model->save()) {
                $this->redirect(array('list'));
            }
        }

        $this->render('create', array(
            'model' => $model,
        ));
    }

    public function actionUpdate($id) {
        $model = $this->loadModel($id);

        $old_thumbnail_filename = $model->products_thumbnail;
        $old_image_filename = $model->products_image;

        if (isset($_POST['site_products'])) {
            $model->attributes = $_POST['site_products'];

            //thumbnail upload
            $thumbnail_file = CUploadedFile::getInstance($model, 'products_thumbnail');
            $thumbnail_filename = '';
            if ((is_object($thumbnail_file) && get_class($thumbnail_file) === 'CUploadedFile')) {
                $thumbnail_filename = function_lib::url_title($thumbnail_file->name);
                $model->products_thumbnail = CUploadedFile::getInstance($model, 'products_thumbnail');
                $model->products_thumbnail = $thumbnail_filename;
            }
            
            //image upload
            $file = CUploadedFile::getInstance($model, 'products_image');
            $filename = '';
            if ((is_object($file) && get_class($file) === 'CUploadedFile')) {
                $filename = function_lib::url_title($file->name);
                $model->products_image = CUploadedFile::getInstance($model, 'products_image');
                $model->products_image = $filename;
            }

            if ($model->save()) {
                //thumbnail upload
                if (is_object($thumbnail_file)) {
                    $model->products_thumbnail = CUploadedFile::getInstance($model, 'products_thumbnail');
                    if ($model->products_thumbnail->saveAs(Yii::app()->params['config']['updir']['products'] . $thumbnail_filename)) {
                        if (file_exists(Yii::app()->params['config']['updir']['products'] . $old_thumbnail_filename)) {
                            //unlink(Yii::app()->params['config']['updir']['products'] . $old_thumbnail_filename);
                        }
                    }
                }
                
                //image upload
                if (is_object($file)) {
                    $model->products_image = CUploadedFile::getInstance($model, 'products_image');
                    if ($model->products_image->saveAs(Yii::app()->params['config']['updir']['products'] . $filename)) {
                        if (file_exists(Yii::app()->params['config']['updir']['products'] . $old_filename)) {
                            //unlink(Yii::app()->params['config']['updir']['products'] . $old_filename);
                        }
                    }
                }
                
                //$this->redirect(array('update', 'id' => $model->products_id));
                $this->redirect(array('list'));
            }
        }

        $this->render('update', array(
            'model' => $model,
        ));
    }

    public function actionDelete($id) {
        if (Yii::app()->request->isPostRequest) {
            // we only allow deletion via POST request
            $this->loadModel($id)->delete();

            // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
            if (!isset($_GET['ajax']))
                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
        }
        else
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
    }

    public function actionIndex() {
        $dataProvider = new CActiveDataProvider('site_products');
        $this->render('index', array(
            'dataProvider' => $dataProvider,
        ));
    }

    public function actionList() {
        $model = new site_products('search');
        $model->unsetAttributes();  // clear any default values
        if (isset($_GET['site_products']))
            $model->attributes = $_GET['site_products'];

        $this->render('list', array(
            'model' => $model,
        ));
    }

    public function loadModel($id) {
        $model = site_products::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'site-products-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

}
